import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import net.miginfocom.swing.*;
/*
 * Created by JFormDesigner on Mon Dec 09 15:39:33 CST 2024
 */



/**
 * @author jjchen
 */
public class login_manager extends JFrame {
    public login_manager() {
        initComponents();
    }

    private void button1MouseClicked(MouseEvent e) {
        manager_Home manager = new manager_Home();
        manager.setVisible(true);
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    static String managerid;
    private void button1_check(ActionEvent e) {
        if (e.getSource() == button1) {
            // 获取用户输入的账户和密码
            String manager_ID = textField1.getText();
            String password = new String(passwordField1.getPassword());

            // 连接到数据库并验证用户
            if (validateUser(manager_ID, password)) {
                JOptionPane.showMessageDialog(this, "管理员登录成功！");
                manager_Home manager = new manager_Home();
                manager.setVisible(true);
                this.dispose();
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                managerid=manager_ID;
            } else {
                JOptionPane.showMessageDialog(this, "登录失败，请检查管理员id和密码！");
            }
        }

    }

    private boolean validateUser(String manager_ID, String password) {
        try {

            // 连接到 MySQL 数据库
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaproject", "root", "root");

            // 查询用户表
            String query = "SELECT * FROM manager_information WHERE manager_ID = ? AND password = ?";
            try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
                preparedStatement.setString(1, manager_ID);
                preparedStatement.setString(2, password);

                // 执行查询
                ResultSet resultSet = preparedStatement.executeQuery();

                // 如果有匹配的记录，则验证通过
                return resultSet.next();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "数据库连接或查询出现问题：" + ex.getMessage());
            return false;
        }
    }

    private void button2MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        manager_register managerRegister=new manager_register();
        managerRegister.setVisible(true);
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label3 = new JLabel();
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        passwordField1 = new JPasswordField();
        button1 = new JButton();
        button2 = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- label3 ----
        label3.setText("\u7ba1\u7406\u5458\u767b\u5f55");
        contentPane.add(label3, "cell 8 0");

        //---- label1 ----
        label1.setText("id");
        contentPane.add(label1, "cell 2 2");
        contentPane.add(textField1, "cell 6 2 5 1");

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2, "cell 2 5");
        contentPane.add(passwordField1, "cell 6 5 5 1");

        //---- button1 ----
        button1.setText("\u767b\u5f55");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        button1.addActionListener(e -> button1_check(e));
        contentPane.add(button1, "cell 7 8");

        //---- button2 ----
        button2.setText("\u6ce8\u518c");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2, "cell 9 8");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label3;
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JPasswordField passwordField1;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
